<template>
     <el-select v-model="modelId" :placeholder="t('aigcApp.inputModelIdTip')" clearable @change="handlerChange">
        <el-option-group v-for="(item,index) in providerModels" :key="item.provider" :label="item.providerName">
            <el-option :key="index" :label="model.modelName" :value="model.id"
                v-for="(model, index) in item.models">
            </el-option>
        </el-option-group>

    </el-select>
</template>

<script lang="ts" setup name="modelSelect">

    import {findList} from "/@/api/aigc/model";
    import {findProviderList} from "/@/api/aigc/providerModel";
    import {ModelTypeEnum,ProviderModel,AigcModel} from "/@/types/aigc/chat";
    import { useI18n } from "vue-i18n";
    import {useChatStore} from "/@/stores/aigc/chatStore";

    const { t } = useI18n();
    const chatStore = useChatStore();
    const providerModels = ref<ProviderModel[]>([]);
    const modelId=ref("");
    //触发事件
    const emit = defineEmits(['update', 'load']);
    const props=defineProps({
        id:String,
        size:String,
    });
    onMounted(()=>{
        //获取供应商信息
      getProviders();
    });
    //获取供应商信息
    const getProviders=async ()=>{
        // 加载模型列表和提供者列表
         findProviderList(ModelTypeEnum.CHAT).then(res=>{
             if(res.success){
                 res.data.forEach((item: any) => {
                     let data = {
                         provider:item.provider,
                         providerName: item.providerName,
                         models:[],
                     };
                     providerModels.value.push(data);
                 });
				//获取模型列表
				getModels();
             }
         });
    };
    //获取模型列表
    const getModels=async ()=>{
       //获取模型
       findList({
           modelType:ModelTypeEnum.CHAT
       }).then(res=>{
           if (res.success) {
               let models = res.data;
               models.forEach((item:any)=>{
                   const providerIndex=providerModels.value.findIndex(p=>p.provider===item.provider);
                   if (providerIndex > -1) {
                       let aigcMode:AigcModel = {
                           id:item.id,
                           name:item.name,
                           modelName:item.modelName,
                       };
                       providerModels.value[providerIndex].models.push(aigcMode);
                   }
               });
               //如果为空
               if (chatStore.modelId===''||chatStore.modelId===null) {
                   if (providerModels!==null&&providerModels.value.length>0) {
                       let providerModel = providerModels.value[0];
                       chatStore.modelProvider = providerModel.provider;
                       if (providerModel.models!=null&&providerModel.models.length>0) {
                           let model = providerModel.models[0];
                           chatStore.modelId = model.id;
                           chatStore.modelName = model.modelName;
                       }
                       //
                       if (props.id==null) {
                           modelId.value = chatStore.modelId;
                           emit('load', providerModel);
                       } else {
                           modelId.value = props.id;
                       }
                }
              } 
              modelId.value = chatStore.modelId;
          }
       });
    };
    //选中值
    const handlerChange=(value:any)=>{
        console.log(value);
        //更新数据
        const updateData={};
        //获取选中的数据
        providerModels.value.find((item:any)=>{
            if (item.models!=null){
              const modelData=item.models.find((model:any)=>{
                    if (model.id===value) {
                        chatStore.modelId = value;
                        chatStore.modelProvider = item.provider;
                        chatStore.modelName = model.modelName;

                        updateData['modelId'] = chatStore.modelId;
                        updateData['modelProvider'] = chatStore.provider;
                        updateData['modelName'] = chatStore.modelName;
                        return model;
                    }
                });
                if(modelData&&modelData.modelId){
                    return modelData;
                }
            }
        });
        emit('update',updateData);
    };
</script>